package transaction.impl;

import exception.DataAccessException;
import factory.annotation.Component;
import transaction.TransactionManager;
import util.JdbcUtil;

import java.sql.Connection;

@Component("transaction")
public class TransactionManagerImpl implements TransactionManager {
    @Override
    public void begin() {
        Connection con=null;
        try {
            con= JdbcUtil.getConnection();

            con.setAutoCommit(false);

        } catch (Exception e) {
            throw new DataAccessException("数据访问失败",e);
        }
    }

    @Override
    public void commit() {
        Connection con=null;
        try{
            con=JdbcUtil.getConnection();

            con.commit();
        } catch (Exception e) {
            throw new DataAccessException("数据访问失败",e);
        } finally {
            JdbcUtil.close();
        }
    }

    @Override
    public void rollback() {
        Connection con=null;
        try{
            con=JdbcUtil.getConnection();

            con.rollback();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close();
        }
    }
}
